const Router = require('@koa/router');
const dictController = require('../../controllers/dictController');
const { authenticate } = require('../../middleware/authMiddleware');

const router = new Router({
    prefix: '/api/dicts'
});

/**
 * @swagger
 * /api/dicts/name/{name}:
 *   get:
 *     summary: 根据名称获取字典
 *     tags: [Dictionary]
 *     parameters:
 *       - in: path
 *         name: name
 *         required: true
 *         schema:
 *           type: string
 *         description: 字典名称
 *     responses:
 *       200:
 *         description: 成功获取字典
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 name:
 *                   type: string
 *                 description:
 *                   type: string
 *                 items:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       label:
 *                         type: string
 *                       value:
 *                         type: string
 *                       sort:
 *                         type: number
 *                       status:
 *                         type: boolean
 *       404:
 *         description: 字典不存在
 */
router.get('/name/:name', dictController.getDictByName);

/**
 * @swagger
 * /api/dicts:
 *   post:
 *     summary: 创建字典
 *     tags: [Dictionary]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *               - items
 *             properties:
 *               name:
 *                 type: string
 *               description:
 *                 type: string
 *               items:
 *                 type: array
 *                 items:
 *                   type: object
 *                   required:
 *                     - label
 *                     - value
 *                   properties:
 *                     label:
 *                       type: string
 *                     value:
 *                       type: string
 *                     sort:
 *                       type: number
 *                     status:
 *                       type: boolean
 *     responses:
 *       201:
 *         description: 字典创建成功
 *       400:
 *         description: 请求参数错误
 */
router.post('/', authenticate, dictController.createDict);

/**
 * @swagger
 * /api/dicts:
 *   get:
 *     summary: 获取所有字典
 *     tags: [Dictionary]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 10
 *         description: 每页显示数量
 *       - in: query
 *         name: name
 *         schema:
 *           type: string
 *         description: 字典名称搜索关键词
 */
router.get('/', authenticate, dictController.getDicts);

/**
 * @swagger
 * /api/dicts/{id}:
 *   put:
 *     summary: 更新字典
 *     tags: [Dictionary]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 字典ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *               description:
 *                 type: string
 *               items:
 *                 type: array
 *                 items:
 *                   type: object
 *                   properties:
 *                     label:
 *                       type: string
 *                     value:
 *                       type: string
 *                     sort:
 *                       type: number
 *                     status:
 *                       type: boolean
 *     responses:
 *       200:
 *         description: 字典更新成功
 *       404:
 *         description: 字典不存在
 */
router.put('/:id', authenticate, dictController.updateDict);

/**
 * @swagger
 * /api/dicts/{id}:
 *   delete:
 *     summary: 删除字典
 *     tags: [Dictionary]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 字典ID
 *     responses:
 *       204:
 *         description: 字典删除成功
 *       404:
 *         description: 字典不存在
 */
router.delete('/:id', authenticate, dictController.deleteDict);

module.exports = router; 